<template>
  <transition name="slide-fade">
    <div class="login" v-if="show">
      <div class="login-cover"></div>
      <div class="login-form col">
        <div class="login-form-title">用户登陆</div>
        <el-form :label-position="labelPosition" label-width="0px" :model="formLabelAlign" ref="formLabelAlign">
          <el-form-item>
            <el-input v-model="formLabelAlign.name" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="formLabelAlign.password" placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item style="margin-top:60px;">
            <el-button type="primary" @click="submitForm('formLabelAlign')">登陆</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      formLabelAlign: {
        name: "",
        password: ""
      },
      labelPosition: "right"
    };
  },
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    submitForm(formName) {
      console.log(formName);
      let _this=this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$message({
            message: "登录成功",
            type: "success"
          });
           _this.$http.post('/index/getToken',_this.formLabelAlign).then(res=>{
             console.log(res)
             let token=res.data.data.token;
             this.$store.commit('updateToken',token);
             if(this.$store.state.token){
                this.$router.addRoutes(this.$store.state.routers);
             }
             _this.$router.push({
               name:'mains'
             })
           }).catch(err=>{
               console.log(err)
           })
           
          // setTimeout(() => {
          //   this.$router.push({
          //     name: "mains"
          //   });
          // }, 1000);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.slide-fade-enter-active {
  transition: all 0.3s ease;
}
.slide-fade-leave-active {
  transition: all 0.8s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-enter,
.slide-fade-leave-to {
  opacity: 0;
}
.login {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99999;
}
.login-cover {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  opacity: 0.7;
  background-color: #000;
}
.login-form {
  width: 400px;
  height: 400px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  background-color: #fff;
}
.login-form-title {
  height: 50px;
  line-height: 50px;
  background: #3fb2ef;
  font-size: 20px;
  text-indent: 10px;
}
.el-form {
  margin-top: 30px;
  padding: 10px 50px;
}
.el-button {
  width: 100%;
}
</style>
